package com.shop.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.shop.entity.Result;
import com.shop.mapper.UserMapper;
import com.shop.pojo.User;
import com.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**	1.登录*/
    @Override
    public User login(User user) {
        if (user == null || StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){
            return null;
        }
        User user1 = userMapper.selectUserByUsername(user.getUsername());
        if (user1 != null && user1.getPassword().equals(user.getPassword())) {
            return user1;
        }
        return null;
    }

    /**2.注册*/
    @Override
    public Result Register(User user) {
        Result result = null;
        if (user == null
                || StringUtils.isEmpty(user.getUsername())
                || StringUtils.isEmpty(user.getPassword())) {
            result = new Result(false, "用户名和密码不能为空");
            return result;
        }
        // 判断用户名是否存在
        User tbUser = userMapper.selectUserByUsername(user.getUsername());
        if (tbUser == null || StringUtils.isEmpty(tbUser.getId())) {
            user.setId(UUID.randomUUID().toString());
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            userMapper.insert(user);
            result = new Result(true, "注册成功", user);
        } else {
            result = new Result(false, "用户名已存在");
        }
        return result;
    }

    /**3.根据id查询用户信息*/
    @Override
    public User getUserById(String uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        return user;
    }
}
